
package Swing;

import common.ReservationUtils;
import entities.Reservation;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class ReservationScreen {
    Object[][] rows;
    
    public void showResponseAfterSaveReservation(String message, int response){
        String dialogHeader = "Zapis terminu naprawy";
        if(response == 1){
            JOptionPane.showMessageDialog(null, message, dialogHeader, JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(null, message, dialogHeader, JOptionPane.ERROR_MESSAGE);   
        }
    }
    
 public void populateReservationTable(JTable ReservationTable, JPanel ReservationPanel, 
         final JTextField reservationOptTxtField){
     String[] columns = { "ID", "Marka", "Imię", "Nazwisko", "Status", "Termin naprawy", "Opis usterki"};
     ArrayList<Reservation> reservationData = new ReservationUtils().getReservationData();
     rows = new Object[reservationData.size()][columns.length];
     for(int i = 0 ; i < reservationData.size() ; i++){
        rows[i][0] = reservationData.get(i).getReservationId();
        rows[i][1] = reservationData.get(i).getCar().getMark();
        rows[i][2] = reservationData.get(i).getCustomer().getFirstName();
        rows[i][3] = reservationData.get(i).getCustomer().getLastName();
        rows[i][4] = reservationData.get(i).getStatus().getName();
        rows[i][5] = reservationData.get(i).getRepairDate();
        rows[i][6] = reservationData.get(i).getDescription();
     }
     TableModel reservationModel = new DefaultTableModel(rows, columns);
     ReservationTable.setModel(reservationModel); 
     final TableRowSorter<TableModel> sorter;
     sorter = new TableRowSorter<>(reservationModel);
     ReservationTable.setRowSorter(sorter);
     ReservationPanel.repaint();     
     
     if(reservationOptTxtField != null){
         reservationOptTxtField.getDocument().addDocumentListener(new DocumentListener() {

             @Override
             public void insertUpdate(DocumentEvent e) {
                String expr = reservationOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }
             @Override
             public void removeUpdate(DocumentEvent e) {
                String expr = reservationOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }
             @Override
             public void changedUpdate(DocumentEvent e) {
                String expr = reservationOptTxtField.getText();
                sorter.setRowFilter(RowFilter.regexFilter(expr));
                sorter.setSortKeys(null);
             }
         });         
     }
 } 
 
  public void editReservationData(JFrame frame, final JTabbedPane MainTabbedPane, final int reservationId){
        if(reservationId > 0){
           ReservationDialog reservationDialog = new ReservationDialog(frame, reservationId, true);
           reservationDialog.setVisible(true);
        }
  } 
}
